#include "mychart.h"
#include <QtCharts/QAbstractAxis>
#include <QtCharts/QSplineSeries>
#include <QtCharts/QValueAxis>
#include <QtCore/QRandomGenerator>
#include <QtCore/QDebug>

/*
 * 画线的时候 添加 Tams >  Deltak * Tass 这道红线

*/



MyChart::MyChart(QGraphicsItem *parent, Qt::WindowFlags wFlags):
    QChart(QChart::ChartTypeCartesian, parent, wFlags),
    m_axisX(new QDateTimeAxis()),
    m_axisY(new QValueAxis())
{
    //QObject::connect(&m_timer, &QTimer::timeout, this, &MyChart::handleTimeout);

    /*曲线初始化以及绑定*/
    m_series = new QSplineSeries(this);
    Tass_series = new QLineSeries(this);
    addSeries(m_series);
    addSeries(Tass_series);

    addAxis(m_axisX,Qt::AlignBottom);
    addAxis(m_axisY,Qt::AlignLeft);
    m_series->attachAxis(m_axisX);
    m_series->attachAxis(m_axisY);
    Tass_series->attachAxis(m_axisX);
    Tass_series->attachAxis(m_axisY);
    m_series->setUseOpenGL(true);
    /*数据线*/
    QPen green_p(Qt::green);
    green_p.setWidth(1);
    m_series->setPen(green_p);
    /*Tass -- 参考线 */
    QPen red_p(Qt::red);
    red_p.setWidth(2);
    red_p.setStyle(Qt::DotLine);
    this->Tass_series->setPen(red_p);

    /*默认值*/
    //时间轴初始化
    m_axisX->setFormat("hh:mm:ss");
    startDateTime = QDateTime(QDate::currentDate(),QTime(7,0,0,0),Qt::LocalTime,1);
    endDateTime = startDateTime.addSecs(12*3600);
    m_axisX->setRange(startDateTime,endDateTime); //12个小时
    m_axisX->setTickCount(13);   //划分时间轴-12个格子
    m_axisY->setRange(-20,500);
    m_axisY->setTickCount(11);  //划分Y轴-10个格子

}

MyChart::~MyChart()
{
    delete  m_axisX;
    delete m_axisY;
    delete m_series;
    delete Tass_series;
}

/*test -- 数据生成 超时 滚动一格*/
void MyChart::handleTimeout()
{
    static quint64 sec = 0;
    //qreal x = plotArea().width() / m_axisX->tickCount();
    //this->scroll(x/10, 0);
    sec++;
    setDateTime = startDateTime.addSecs(sec);
    this->DrawNewPoint(QRandomGenerator::global()->bounded(5));

}

void MyChart::setSunBreakingReferenceLine(qreal value)
{
    this->Tass_series->clear();
    this->Tass_series->append(startDateTime.toMSecsSinceEpoch(),value);
    this->Tass_series->append(startDateTime.addSecs(3600*12).toMSecsSinceEpoch(),value);
}


bool MyChart::isExistPoint()
{
    if(m_series->count() == 0){
        return true;
    }else{
        return false;
    }
}


/*
void MyChart::startAutoScroll(int interval)
{
    m_timer.start(interval);
    ScrollModFlag = true;
}

void MyChart::stopAutoScroll()
{
    ScrollModFlag = false;
    m_timer.stop();
}



void MyChart::startMaxAutoIncrease(int interval)
{
    m_timer.start(interval);
    MaxAutoIncreaseModFlag = true;
}

void MyChart::stopMaxAutoIncrease()
{
    MaxAutoIncreaseModFlag = false;
    m_timer.stop();
}


*/

void MyChart::scrollOneTick()
{
    qreal x = plotArea().width() /(m_axisX->tickCount()-1);
    this->scroll(x, 0);
}

void MyChart::MaxIncreaseOneTick()
{
    if(endDateTime.time().hour() < 20){ //最大到晚八点--可持续调整
        endDateTime = endDateTime.addSecs(3600);
        this->setAxisX_MaxRange();
    }
}

void MyChart::setAxisXRange()
{
    m_axisX->setRange(startDateTime,endDateTime);
}

void MyChart::setAxisX_MaxRange()
{
    m_axisX->setMax(endDateTime);
}


void MyChart::setAxisYRange(qreal &min,qreal &max)
{
    m_axisY->setRange(min, max);
}


void MyChart::setAxisXTickCount(int tickcount)
{
    m_axisX->setTickCount(tickcount);
}

void MyChart::setAxisYTickCount(int tickcount)
{
    m_axisY->setTickCount(tickcount);
}


/*  Qt::red Qt::green  */
void MyChart::setSerialColorAndWidth(const QColor &color,int w)
{
    QPen p(color);
    p.setWidth(w);
    m_series->setPen(p);
}


/*画点函数 执行之前需要设定时间点 setDateTime */
void  MyChart::DrawNewPoint(qreal value)
{
    this->m_series->append(setDateTime.toMSecsSinceEpoch(),value);
}

void  MyChart::ScrollParaInit()
{
    //范围除以格子数量得到每格有多少秒
    SecNumPerTick = (m_axisX->min().secsTo(m_axisX->max()) / (m_axisX->tickCount()-1));
}


void  MyChart::DrawNewPointwithScrollOneTick(qreal value)
{
    this->DrawNewPoint(value);

    //超过10*N 格子 滚动一次 并且去掉N个点
    if(m_series->count() > (10*SecNumPerTick)){
        m_series->removePoints(0,SecNumPerTick-1);//移除五个点
        this->scrollOneTick();

    }//小于7格 的不更新滚动

}


void  MyChart::DrawNewPointwithSetMaxRange(qreal value)
{
    this->DrawNewPoint(value);

    if(m_series->count() > (startDateTime.secsTo(endDateTime)) ){
        if(endDateTime.time().hour() < 20){ //最大到晚八点--可持续调整
            endDateTime = endDateTime.addSecs(3600);
            this->setAxisX_MaxRange();
        }
    }
}





/*时间轴运用！*/

//获取当前时间点 还是解析数据包中的时间点 ？？？



